package com.rambo.baselib.distribute;

import com.rambo.baselib.util.LogUtil;
import com.rambo.baselib.util.ToastUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.continuation.*;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.distributedschedule.interwork.IInitCallback;
import ohos.rpc.RemoteException;

import java.util.List;

/***********************************************************************
 * <p>@description: 跨设备流转设备选择器
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/17 14:19
 **********************************************************************/
public class DeviceSelector implements IContinuationDeviceCallback, RequestCallback, IInitCallback {
    private int token;
    private boolean isSetUp;
    private DeviceInfo deviceInfo;
    private SelectDeviceResultListener selectDeviceResultListener;
    private IContinuationRegisterManager iContinuationRegisterManager;

    public DeviceSelector() {

        deviceInfo = new DeviceInfo();
    }

    public void setUp(Ability ability) {
        if (!isSetUp) {
            isSetUp = true;
            iContinuationRegisterManager = ability.getContinuationRegisterManager();
            iContinuationRegisterManager.register(ability.getBundleName(), null, this, this);
        }
    }

    public void destroy() {
        if (!isSetUp) return;
        isSetUp=false;
        iContinuationRegisterManager.unregister(token, null);
        iContinuationRegisterManager.disconnect();
    }

    public void showDistributeDevices(String[] devTypes, String ext) {
        if (isSetUp) {
            // 设置过滤设备类型
            ExtraParams params = new ExtraParams();
            params.setDevType(devTypes);
            params.setJsonParams(ext);
            // 显示选择设备列表
            iContinuationRegisterManager.showDeviceList(token, params, null);
        } else {
            ToastUtil.toast("分布式服务未初始化");
        }
    }

    public void setSelectDeviceResultListener(SelectDeviceResultListener listener) {
        selectDeviceResultListener = listener;
    }

    private void setRemoteDeviceInfo(String selectedId) {
        List<DeviceInfo> deviceList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        if (deviceList != null) {
            for (DeviceInfo d : deviceList) {
                if (d.getDeviceId().equals(selectedId)) {
                    deviceInfo = d;
                    return;
                }
            }
        }
        if (deviceInfo != null) {
            deviceInfo.setDeviceInfo(selectedId, "");
        }
    }

    @Override
    public void onDeviceConnectDone(String deviceId, String deviceType) {
        /**
         * 用户选择设备后回调
         */
        setRemoteDeviceInfo(deviceId);
        try {
            DeviceManager.initDistributedEnvironment(deviceId, this);
        } catch (RemoteException e) {
            e.printStackTrace();
            LogUtil.e("===onDeviceConnectDone ==="+e.getMessage());
        }
        iContinuationRegisterManager.updateConnectStatus(token, deviceId, DeviceConnectState.DIS_CONNECTING.getState(), null);
    }

    @Override
    public void onDeviceDisconnectDone(String deviceId) {
        /**
         * 流转中断设备已断开
         */
        iContinuationRegisterManager.updateConnectStatus(token, deviceId, DeviceConnectState.IDLE.getState(), null);
    }

    @Override
    public void onResult(int result) {
        /**
         * 与流转任务管理服务交互成功时调用
         */
        token = result;
        LogUtil.e("==onResult=="+result);
    }

    @Override
    public void onInitSuccess(String deviceId) {
        LogUtil.e("==onInitSuccess=="+deviceId);
        /**
         * 分布式设备初始化成功
         */
        if (selectDeviceResultListener != null) {
            selectDeviceResultListener.onSuccess(deviceInfo);
        }
    }

    @Override
    public void onInitFailure(String deviceId, int errorCode) {
        LogUtil.e("==onInitFailure=="+errorCode);
        /**
         * 分布式设备初始化失败
         */
        if (selectDeviceResultListener != null) {
            selectDeviceResultListener.onFail(deviceInfo);
        }
    }
}
